﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWebPagesStarterKit;
using MyWebPagesStarterKit.Controls;
using System.IO;
using System.Web.UI.HtmlControls;

public partial class SectionControls_CodeColorizer : SectionControlBaseClass
{
    private CodeColorizer _section;
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }

    private void FillList()
    {
        string[] files = Directory.GetFileSystemEntries(Server.MapPath("/Scripts/sh/"),"*.js");
        foreach (string file in files)
            codeSelect.Items.Add(new ListItem(file.Replace("shBrush", ""), file));
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        updateViews();
    }

    protected void SaveCode(object sender, EventArgs e)
    {
        if (txtHtml.Text != string.Empty)
        {
            if (codeSelect.SelectedValue != string.Empty)
            {
                _section.Code = codeSelect.SelectedValue;
                _section.Html = txtHtml.Text;
                _section.SaveData();
            }
        }
    }

    public override ISection Section
    {
        set
        {
            if (value is CodeColorizer)
                _section = (CodeColorizer)value;
            else
                throw new ArgumentException("Section must be of type CodeColorizer");
        }
        get { return _section; }
    }

    public override bool HasAdminView
    {
        get { return true; }
    }

    public override string InfoUrl
    {
        get
        {
            string lang = System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;
            if (File.Exists(HttpContext.Current.Server.MapPath(string.Format("Documentation/" + lang + "/quick_guide.html"))))
            {
                return "Documentation/" + lang + "/quick_guide.html#contact-form";
            }
            else
            { return "Documentation/en/quick_guide.html#contact-form"; }
        }
    }

    private void updateViews()
    {
        if (ViewMode == ViewMode.Edit)
        {
            multiview.SetActiveView(editView);
            txtHtml.Text = _section.Html;
        }
        else
        {
            multiview.SetActiveView(readonlyView);

            HtmlLink css = new HtmlLink();
            css.Attributes.Add("href", "/scripts/sh/shCore.css");
            css.Attributes.Add("media", "screen");
            css.Attributes.Add("rel", "stylesheet");
            css.Attributes.Add("type", "text/css");

            HtmlLink css2 = new HtmlLink();
            css2.Attributes.Add("href", "/scripts/sh/shThemeDefault.css");
            css2.Attributes.Add("media", "screen");
            css2.Attributes.Add("rel", "stylesheet");
            css2.Attributes.Add("type", "text/css");

            Page.Header.Controls.Add(css);
            Page.Header.Controls.Add(css2);

            litHtml.Text = string.Format("<pre class=\"brush: {0}\">{1}</pre>", _section.Code, _section.Html);
            
            string scripts = "<script src=\"/Scripts/sh/shCore.js\" type=\"text/javascript\"></script>\n";

            string[] files = Directory.GetFileSystemEntries(Server.MapPath("/Scripts/sh/"),"*.js");
            foreach (string file in files)
            {
                string src = file.Substring(file.LastIndexOf("\\")+1);
                if (!src.Equals("shCore.js"))
                    if (src.EndsWith(".js"))
                        scripts += string.Format("<script src=\"/Scripts/sh/{0}\" type=\"text/javascript\"></script>\n", src);
            }
            scripts += "<script type=\"text/javascript\">\n"
            + "    SyntaxHighlighter.config.clipboardSwf = '/Scripts/sh/clipboard.swf';\n"
            + "    SyntaxHighlighter.all();\n"
            + "</script>\n";

            Page.ClientScript.RegisterStartupScript(this.GetType(),"Highlighter", scripts);
        }
    }

}
